package com.doudanhua.basecode.jackson;

import com.doudanhua.basecode.jackson.configurer.DateTimeJsonConfigurer;
import com.doudanhua.basecode.jackson.configurer.DefaultJsonConfigurer;
import com.doudanhua.basecode.jackson.configurer.JsonConfigurer;
import com.doudanhua.basecode.jackson.time.DateTimeConverter;
import com.doudanhua.basecode.jackson.time.NormalDateTimeConverter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhangqian
 * @Description TODO
 * @Date 2022/11/4 16:48
 */
public class JsonConvert {
    private final static ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();

        List<JsonConfigurer> configurers = new ArrayList<>();
        configurers.add(new DateTimeJsonConfigurer());
        configurers.add(new DefaultJsonConfigurer());
        for (JsonConfigurer configurer : configurers) {
            configurer.config(objectMapper);
        }
    }


    public static ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    public static TypeFactory getTypeFactory() {
        return objectMapper.getTypeFactory();
    }

    public static String toJson(Object data) {
        if (data == null)
            return "";
        try {
            return objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T fromJson(String str, Class<T> type) {
        if (StringUtils.isEmpty(str))
            return null;
        try {
            return objectMapper.readValue(str, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T fromJson(String str, JavaType type) {
        if (StringUtils.isEmpty(str))
            return null;
        try {
            return objectMapper.readValue(str, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T fromJson(String str, TypeReference type) {
        if (StringUtils.isEmpty(str))
            return null;
        try {
            return (T) objectMapper.readValue(str, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static byte[] toByte(Object data) {
        if (data == null)
            return new byte[0];
        try {
            return objectMapper.writeValueAsBytes(data);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    public static <T> T fromByte(byte[] data, Class<T> type) {
        if (data == null)
            return null;
        try {
            return objectMapper.readValue(data, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
